package com.linkoog.devtools.http.toolwindow.workbenche.left.http_service;

import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiMethod;
import com.intellij.ui.JBColor;
import com.intellij.ui.treeStructure.SimpleTree;
import com.intellij.util.OpenSourceUtil;
import com.linkoog.devtools.http.toolwindow.workbenche.left.http_service.node.ClassTreeNode;
import com.linkoog.devtools.http.toolwindow.workbenche.left.http_service.node.MethodTreeNode;
import com.linkoog.devtools.intellij.ui.tree.ListTreeWithToolbarPanel;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.util.List;

public class HttpServicePanel extends ListTreeWithToolbarPanel implements Disposable {

    private final Project project;
    private final HttpServiceTreeDataProvider myDataBuilder;

    public static final String SHOW_SWAGGER_KEY = "com.github.linkoog.ApiDevTools.showSwaggerView";


    public HttpServicePanel(Project project, Disposable parentDisposable){
        super(new SimpleTree(), new HttpServiceTreeCellRenderer(), new HttpServiceTreeToolbarActionsProvider(project), parentDisposable);
        this.project = project;
        this.myDataBuilder = new HttpServiceTreeDataProvider(project);
        getTree().setRootVisible(false);

        chooseListener = (tree, treeNode) -> {
        };

        doubleClickListener = (tree, treeNode) -> {
            if (treeNode instanceof ClassTreeNode classTreeNode) {
                final PsiClass psiClass = classTreeNode.getHttpServiceClass().getPsiElement();
                OpenSourceUtil.navigate(true, psiClass);
            } else if (treeNode instanceof MethodTreeNode methodTreeNode){
                final PsiMethod psiMethod = methodTreeNode.getHttpServiceMethod().getPsiElement();
                OpenSourceUtil.navigate(true, psiMethod);
            }
        };

        HttpServiceTreeMenuProvider treeMenuProvider = new HttpServiceTreeMenuProvider(project, getTree());
        rightClickMenuProvider = treeMenuProvider::createNodePopupMenu;

        refreshRestServiceTree();
    }

    public void loadHttpServiceTreeData(){
        DefaultMutableTreeNode rootNode = myDataBuilder.buildHttpServiceData();
        SwingUtilities.invokeLater(() -> {
            DefaultTreeModel model = new DefaultTreeModel(rootNode);
            getTree().setModel(model);
        });
    }

    public void resetSelectedModuleList(List<String> selectedValues) {
        myDataBuilder.resetSelectedModuleList(selectedValues);
    }

    public void refreshRestServiceTree(){
        ApplicationManager.getApplication().executeOnPooledThread(() -> ProgressManager.getInstance().run(new Task.Backgroundable(project, "Loading Http Service Tree ..."){

            @Override
            public void run(@NotNull ProgressIndicator progressIndicator) {
                try {
                    getTree().setEnabled(false);
                    setLoadingText("waiting to load data!");
                    startLoading();
                    setBackground(JBColor.GRAY);

                    loadHttpServiceTreeData();
                }finally {
                    stopLoading();
                    getTree().setEnabled(true);
                }
            }
        }));
    }




    @Override
    public void dispose() {

    }


}
